package com.flute.framework.search.result.xmlresult.multi;

import java.util.HashMap;
import java.util.Map;

import com.flute.framework.util.MulRegExUtils;
import com.flute.framework.util.XpathStringUtils;

public abstract class BaseKeyValueXpathParser implements IXpathExParseEnable {
	static final String KEYVALUE_REGEX_STR = "#\\[(.+?=.+?)+\\]";

	protected Map<String, String> getMap(String xpath) {
		Map<String, String> result = new HashMap<String, String>();
		String last = XpathStringUtils.getLastPath(xpath).trim();
		String[] group = last.substring(2, last.length() - 1).split("[ ]+");
		for (String keyvalue : group) {
			String[] content = keyvalue.split("=");
			if (content.length == 2) {
				result.put(content[0], content[1]);
			}
		}
		return result;
	}

	@Override
	public boolean correspond(String xpath) {
		if (XpathStringUtils.isValidXpath(xpath)) {
			String last = XpathStringUtils.getLastPath(xpath).trim();
			return MulRegExUtils.valid(last, KEYVALUE_REGEX_STR);
		}
		return false;
	}
}
